/*
 *   Copyright 1999-2015 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

/**
 * @version v 1.0 on 2015-10-21 10:33
 * @auther william.xu
 */
public class RedisPubSub extends JedisPubSub {

    public void onMessage(String channel, String message) {
        System.out.println("onMessage: channel["+channel+"], message["+message+"]");
    }

    public void onPMessage(String pattern, String channel, String message) {
        System.out.println("onPMessage: channel["+channel+"], message["+message+"]");
    }

    public void onSubscribe(String channel,int subscribedChannels) {
        System.out.println("onSubscribe: channel["+channel+"],"+
                "subscribedChannels["+subscribedChannels+"]");
    }

    public void onUnsubscribe(String channel,int subscribedChannels) {
        System.out.println("onUnsubscribe: channel["+channel+"], "+
                "subscribedChannels["+subscribedChannels+"]");
    }

    public void onPUnsubscribe(String pattern,int subscribedChannels) {
        System.out.println("onPUnsubscribe: pattern["+pattern+"],"+
                "subscribedChannels["+subscribedChannels+"]");
    }

    public void onPSubscribe(String pattern,int subscribedChannels) {
        System.out.println("onPSubscribe: pattern["+pattern+"], "+
                "subscribedChannels["+subscribedChannels+"]");
    }

    public static void main(String[] args) {
        Jedis jr = null;
        try {
            jr = new Jedis("192.168.198.131", 6379, 0);//redis服务地址和端口号
            RedisPubSub sp = new RedisPubSub();
            sp.proceed(jr.getClient(),"mykey1");
            //sp.proceedWithPatterns(jr.getClient(), "news.*");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jr != null) {
                jr.disconnect();
            }
        }
    }
}
